home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
haeberli
/
libgutil
/
hist.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
3KB
|
160 lines
/*
* Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
/*
* hist -
* Support for histogram creation and display.
*
* Paul Haeberli - 1988
*/
#include "hist.h"
#include "gl.h"
histogram *newhist(min,max,nbuckets)
int min, max;
int nbuckets;
{
histogram *hist;
int i, *ptr;
hist = (histogram *)mymalloc(sizeof(histogram));
hist->nbuckets = nbuckets;
hist->bucket = (int *)mymalloc(nbuckets*sizeof(int));
hist->min = min;
hist->max = max;
clearhist(hist);
return hist;
}
freehist(hist)
histogram *hist;
{
if(hist) {
free(hist->bucket);
free(hist);
}
}
clearhist(hist)
histogram *hist;
{
int i, nbuckets, *ptr;
ptr = hist->bucket;
nbuckets = hist->nbuckets;
for (i=0; i<nbuckets; i++)
*ptr++ = 0;
}
addtohist(hist,sptr,n)
histogram *hist;
unsigned short *sptr;
int n;
{
unsigned int index, nbuckets;
int *bucket;
nbuckets = hist->nbuckets;
bucket = hist->bucket;
while (n--) {
index = *sptr++;
if(index>=nbuckets)
index = nbuckets-1;
bucket[index]++;
}
}
showhist(hist)
histogram *hist;
{
int nbuckets;
int i, bmax, j, total, *ptr;
nbuckets = hist->nbuckets;
total = bmax = 0;
ptr = hist->bucket;
for (i=0; i<nbuckets; i++) {
total += *ptr;
if(*ptr>bmax)
bmax = *ptr;
ptr++;
}
if(bmax==0)
total = bmax = 1;
ortho2(0.0,(float)nbuckets,0.0,(float)bmax);
grey(0.4);
clear();
grey(0.5);
ptr = hist->bucket;
for (i=0; i<nbuckets; i++)
rectf(i+0.1,0.0,i+0.9,5.0*(float)(*ptr++));
for (i=0; i<nbuckets; i+=10) {
if((i%100) == 0)
grey(0.0);
else
grey(0.2);
move2i(i,0);
draw2i(i,bmax);
}
grey(0.8);
ptr = hist->bucket;
for (i=0; i<nbuckets; i++)
rectf(i+0.1,0.0,i+0.9,(float)(*ptr++));
rgb(1.0,0.0,0.0);
move2i(hist->min,0);
draw2i(hist->min,bmax/40);
move2i(hist->max,0);
draw2i(hist->max,bmax/40);
ortho2(0.0,(float)nbuckets,0.0,(float)total);
grey(0.0);
move2i(0,0);
j = 0;
ptr = hist->bucket;
for (i=0; i<nbuckets; i++) {
j += *ptr++;
draw2((float)i,(float)j);
}
}
histeqtable(hist,tab)
histogram *hist;
short *tab;
{
int i, sum, shade, nbuckets;
int *bucket;
float maxshade;
nbuckets = hist->nbuckets;
bucket = hist->bucket;
sum = 0;
for(i=0; i<nbuckets; i++)
sum += bucket[i];
if(sum == 0)
sum = 1;
maxshade = 255.0;
shade = 0;
for(i=0; i<nbuckets; i++) {
tab[i] = (shade*255.0)/sum;
shade += bucket[i];
}
}